package us.wili.dev.client.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.wili.dev.client.dao.ClientShipAddressMapper;
import us.wili.dev.client.model.req.ShipAddressReq;
import us.wili.dev.client.model.req.UpAddressReq;
import us.wili.dev.client.model.rsp.ShipAddressRsp;
import us.wili.dev.client.service.ClientShipAddressService;
import us.wili.dev.client.service.ClientUserService;
import us.wili.dev.model.entity.Shipaddress;
import us.wili.dev.model.entity.Shopcart;
import us.wili.dev.model.entity.User;
import us.wili.dev.model.example.ShipaddressExample;
import us.wili.dev.model.example.ShopcartExample;
import us.wili.dev.service.impl.BaseShipAddressServiceImpl;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClientShipAddressServiceImpl extends BaseShipAddressServiceImpl implements ClientShipAddressService {
    @Resource
    private ClientShipAddressMapper clientShipAddressMapper;

    @Autowired
    private ClientUserService clientUserService;

    @Override
    public List<ShipAddressRsp> listaddress(Long id) {
        User user = clientUserService.findUser(id);
        List<Shipaddress> list = shipaddresses(user);
        List<ShipAddressRsp> rsps = new ArrayList<>();
        for (Shipaddress shipaddress : list) {
            ShipAddressRsp rsp = ShipAddressRsp.address(shipaddress);
            rsps.add(rsp);
        }
        return rsps;
    }

    @Override
    public Shipaddress addaddress(ShipAddressReq req) {
        User user = clientUserService.findUser(req.getUserid());
        Shipaddress shipaddress = new Shipaddress();
        shipaddress.setUserid(user.getId());
        shipaddress.setShipname(req.getShipname());
        shipaddress.setShipphone(req.getShipphone());
        shipaddress.setAddress(req.getAddress());
        shipaddress.setAddressDeta(req.getAddressdeta());
        if (req.getDefaultaddress()==0){
            Shipaddress shipaddress1 = searDefault(req);
            if (shipaddress1!=null){
                shipaddress1.setDefaultaddress(1);
                update(shipaddress1.getId(),shipaddress1,new ShipaddressExample());
            }
            shipaddress.setDefaultaddress(req.getDefaultaddress());
        }else {
            shipaddress.setDefaultaddress(req.getDefaultaddress());
        }
        clientShipAddressMapper.insert(shipaddress);
        return shipaddress;
    }

    protected int update(Long id,Shipaddress shopcart,ShipaddressExample example){
        example.createCriteria().andIdEqualTo(id);
        return clientShipAddressMapper.updateByExampleSelective(shopcart,example);
    }

    @Override
    public Shipaddress upaddaddress(UpAddressReq req) {
        Shipaddress shipaddress = searUpShipaddress(req);
        shipaddress.setShipname(req.getShipname());
        shipaddress.setShipphone(req.getShipphone());
        shipaddress.setAddress(req.getAddress());
        shipaddress.setAddressDeta(req.getAddressdeta());
        if (req.getDefaultaddress()==0){
            Shipaddress shipaddress1 = searDefault1(req);
            if (shipaddress1!=null){
                shipaddress1.setDefaultaddress(1);
                update(shipaddress1.getId(),shipaddress1,new ShipaddressExample());
            }
            shipaddress.setDefaultaddress(req.getDefaultaddress());
        }else {
            shipaddress.setDefaultaddress(req.getDefaultaddress());
        }
        update(shipaddress.getId(),shipaddress,new ShipaddressExample());

        return shipaddress;
    }

    @Override
    public void deleteAddress(Long id) {
        if (id!=null){
            ShipaddressExample example = new ShipaddressExample();
            example.createCriteria().andIdEqualTo(id);
            clientShipAddressMapper.deleteByExample(example);
        }
    }

    @Override
    public ShipAddressRsp oneAddress(Long id) {
        Shipaddress shipaddress = findShipaddress(id);
        return ShipAddressRsp.address(shipaddress);
    }

    public Shipaddress findShipaddress(Long id){
        ShipaddressExample example = new ShipaddressExample();
        example.createCriteria().andIdEqualTo(id);
        return clientShipAddressMapper.selectOneByExample(example);
    }

    public Shipaddress searUpShipaddress(UpAddressReq req){
        ShipaddressExample example = new ShipaddressExample();
        example.createCriteria().andIdEqualTo(req.getId());
        return clientShipAddressMapper.selectOneByExample(example);
    }

    public List<Shipaddress> shipaddresses(User user){
        ShipaddressExample example = new ShipaddressExample();
        example.createCriteria().andUseridEqualTo(user.getId());
        return clientShipAddressMapper.selectByExample(example);
    }

    public Shipaddress searDefault(ShipAddressReq req){
        ShipaddressExample example = new ShipaddressExample();
        example.createCriteria().andUseridEqualTo(req.getUserid()).andDefaultaddressEqualTo(0);
        return clientShipAddressMapper.selectOneByExample(example);
    }

    public Shipaddress searDefault1(UpAddressReq req){
        ShipaddressExample example = new ShipaddressExample();
        example.createCriteria().andUseridEqualTo(req.getUserid()).andDefaultaddressEqualTo(0);
        return clientShipAddressMapper.selectOneByExample(example);
    }

}
